#include "mainWindow.h"
#include "global.h"
using namespace std;
MainWindow::~MainWindow()
{
   delete cbPtr;
}
MainWindow::MainWindow()
{
    cbPtr = new ChessBoard;
}

void MainWindow::init()
{
    //MainWindow初始化
     cbPtr->init();//棋子初始化
}

void MainWindow::readSize()
{
    //MainWindow初始化
    //一开始就读取用户输入的棋盘大小
    message = "please input the size of chessboard,such as 5 * 5";
    showMessageBox(message);
    string widths,size1,height;
    cin>>widths>>size1>>height;
    cbPtr->resize(stoi(widths),stoi(height));
    cout<<"width:"<<cbPtr->getWidth()<<"height:"<<cbPtr->getHeight()<<endl;
}

string MainWindow::getScore()
{
    //获取黑白棋子的比分
    int whiteScore,blackScore;
    whiteScore = cbPtr->getWhiteScore();
    blackScore = cbPtr->getBlackScore();
    string str = " VS ";
    char buf[32];
    sprintf(buf," : %d VS %d",blackScore,whiteScore);
    str = COLOR_BLACK + str + COLOR_WHITE + string(buf);
    return str;
}
void MainWindow::update(){}
bool MainWindow::isWinner(int row,int column,ChessColor chessColor){
    ChessPos chessPos(row,column);
    Chess chess(chessPos,chessColor);
    bool ret = cbPtr->isWinner(chess);
    return ret;
}
bool MainWindow::insert(int row,int column,ChessColor chessColor){
    //判断是否越界
    bool ret = cbPtr->isOutranged(row,column);
    if(ret) return false;
    //判断重复
    ret = cbPtr->isRepeat(row,column);
    if(ret) return false;
    ChessPos chessPos(row,column);
    Chess chess(chessPos,chessColor);
    cbPtr->addChess(chess);
    return true;
}

void MainWindow::showChessBoard()
{
    //固定行列
    int row,column,width,height;
    width = cbPtr->getWidth();
    height = cbPtr->getHeight();
    //从第-1行开始
    for(row = -1;row < height; row++)
    {
        if(row == -1)
        {
            //构建第一行的数据
            char buf[32] = {0};
            string firstLine;
            for(column = 0;column < width;column++)
            {
                sprintf(buf,"%5d",column);
                firstLine += buf;
            }
            cout<<firstLine<<endl;
            
        }else if(row == (height -1))
        {
            //构建最后一行的数据
            char buf[32] = {0};
            sprintf(buf,"%2d ",row);
            string lastLine;
            lastLine += buf;
            lastLine += cbPtr->chessColor(row,0);
            for(column =1;column<width;column++)
            {
                string str("---");
                str += cbPtr->chessColor(row,column);
                lastLine += str;
            }
            cout<<lastLine<<endl;

        }else{
            //构建中间行的数据
            char buf[32] = {0};
            sprintf(buf,"%2d ",row);
            string middleLine;
            middleLine += buf;
            middleLine += cbPtr->chessColor(row,0);
            for(column = 1;column < width; column++)
            {
                string str("---");
                str += cbPtr->chessColor(row,column);
                middleLine += str;
            }
            cout<<middleLine<<endl;
            //间隔行
            string intervalLine("   ");
            intervalLine += COLOR_BLANK;
            for(column = 1;column < width; column++)
            {
                string str("   ");
                str += COLOR_BLANK;
                intervalLine += str;
            }
            cout<<intervalLine<<endl;
        }
    }
    
}

void MainWindow::showMessageBox(string message)
{
    cout<<"**************************************"<<endl;
    cout<<message<<endl;
}
void MainWindow::showLastResult()
{

}
void MainWindow::readChoice(int* choice)
{
}

void MainWindow::readChessPos(ChessColor chessColor,int* row,int*column)
{

}

void MainWindow::readChessBoardSize(int *rows,int* columns)
{

}


/* int main() */
/* { */
/*     MainWindow *mw = new MainWindow; */
    
/*     mw->init(); */
/*     mw->showChessBoard(); */
    
/* } */

